<template>
	<view class="sqxxQr">
		<!-- 		<view class="pageTitle">
			<text>确认授权</text>
		</view> -->
		<view class="main">
			<view class="title">
				<view class="tm">租赁房屋</view>
			</view>
			<view class="box">
				<view class="item">
					<view class="name">房屋地址</view>
					<view class="val">{{paramsVal.address}}</view>
				</view>
				<view class="item">
					<view class="name">租赁编号</view>
					<view class="val">{{paramsVal.rentCode}}</view>
				</view>
				<view class="item">
					<view class="name">租金所属期</view>
					<view class="val" v-if="paramsVal.startDate">
						{{paramsVal.startDate.slice(0,10)}}至{{paramsVal.endDate.slice(0,10)}}
					</view>
				</view>
			</view>
			<view class="title">
				<view class="tm">授权信息</view>
			</view>
			<view class="box">
				<view class="conSearch">
					<view class="xzItem">
						<view class="name">授权人</view>
						<view class="con">
							<view class="val">{{paramsVal.nsrmc}} ({{paramsVal.nsrzjhm}})</view>
						</view>
					</view>
					<view class="xzItem">
						<view class="name">被授权人</view>
						<view class="con">
							<view class="val">{{paramsVal.czrmc}} ({{paramsVal.czrzjhm}})</view>
						</view>
					</view>
					<view class="xzItem">
						<view class="name">授权租赁所属期起</view>
						<view class="con">
							<view>{{formData.rentSsqq}}</view>
							<pickerTime fields="month" @dateChange="rentSsqqChange"></pickerTime>
						</view>
					</view>
					<view class="xzItem">
						<view class="name">授权租赁所属期止</view>
						<view class="con">
							<view>{{formData.rentSsqz}}</view>
							<pickerTime fields="month" @dateChange="rentSsqzChange"></pickerTime>
						</view>
					</view>
					<view class="xzItem">
						<view class="name">授权有效期限</view>
						<view class="con">
							<view v-if="formData.sqyxqz">{{formData.sqyxqz}}截止</view>
						</view>
					</view>
				</view>
			</view>
			<view class="btn gddw">
				<button type="primary" class="qrbtn" size="mini" @click="sqBtn">授权</button>
			</view>
		</view>
	</view>
</template>

<script>
	import pickerTime from "@/components/pickerTime.vue"
	import commonTools from "@/utils/common-tools.js";
	import {
		saveFpSqxx
	} from "@/utils/common-api.js"
	export default {
		name: "sqxxQr",
		data() {
			return {
				userInfo: commonTools.getUserInfo() || {},
				paramsVal: "",
				formData: {
					rentSsqq: "",
					rentSsqz: "",
					sqyxqz: "",
				},
			};
		},
		components: {
			pickerTime
		},
		onLoad(e) {
			this.paramsVal = JSON.parse(e.data)
			console.log(this.paramsVal)
		},
		methods: {
			// 授权租赁所属期起
			rentSsqqChange(val) {
				this.formData.rentSsqq = val
			},
			// 授权租赁所属期止
			rentSsqzChange(val) {
				this.formData.rentSsqz = val
				this.formData.sqyxqz = this.$commonCzwjs.getNextMonthFE(this.formData.rentSsqz)[1]

			},
			// 格式化租金所属期
			gshZjssq() {
				let rentSsqq = this.$commonCzwjs.getMonthFE(this.formData.rentSsqq)[0]
				let rentSsqz = this.$commonCzwjs.getMonthFE(this.formData.rentSsqz)[1]
				return {
					rentSsqq,
					rentSsqz
				}
			},
			// 时间大小判断
			judgeTime() {
				let judge = false
				if (this.formData.rentSsqq && this.formData.rentSsqz) {
					let zjssq = this.gshZjssq()
					judge = this.$commonCzwjs.compareDate(zjssq.rentSsqq, zjssq.rentSsqz)
				}
				return judge
			},
			// 判断授权时间是否超出租金所属期
			judgeSqsjSfcs() {
				let judge = false
				let zjssq = this.gshZjssq()
				if (
					this.paramsVal.startDate.slice(0, 7).replace("-", "") > zjssq.rentSsqq.slice(0, 7).replace("-", "") ||
					zjssq.rentSsqz.slice(0, 7).replace("-", "") > this.paramsVal.endDate.slice(0, 7).replace("-", "")
				) {
					judge = true
				}

				return judge
			},
			// 授权
			async sqBtn() {
				if (!this.formData.rentSsqq) {
					uni.showModal({
						title: '提示',
						content: '请选择授权租赁所属期起',
						confirmColor: '#000000', //确定字体颜色
						showCancel: false, //没有取消按钮的弹框 
						buttonText: '确定',
						success: function(res) {

						}
					});
					return
				}
				if (!this.formData.rentSsqz) {
					uni.showModal({
						title: '提示',
						content: '请选择授权租赁所属期止',
						confirmColor: '#000000', //确定字体颜色
						showCancel: false, //没有取消按钮的弹框 
						buttonText: '确定',
						success: function(res) {

						}
					});
					return
				}
				if (this.judgeTime()) {
					uni.showModal({
						title: '提示',
						content: '授权租赁所属期起小于或等于授权租赁所属期止',
						confirmColor: '#000000', //确定字体颜色
						showCancel: false, //没有取消按钮的弹框 
						buttonText: '确定',
						success: function(res) {

						}
					});
					return
				}
				if (this.judgeSqsjSfcs()) {
					uni.showModal({
						title: '提示',
						content: '授权租赁所属期超出租金所属期，请前往代征单位申请代开或出租人自行代开',
						confirmColor: '#000000', //确定字体颜色
						showCancel: false, //没有取消按钮的弹框 
						buttonText: '确定',
						success: function(res) {

						}
					});
					return
				}
				let msg = "确认授权代开增值税电子普通发票"
				if (this.paramsVal.nsrlx == '1') {
					msg = "确认申请授权代开增值税电子普通发票"
				}
				uni.showModal({
					title: '提示',
					content: msg,
					confirmColor: '#007aff', //确定字体颜色
					buttonText: '确定',
					success: async (res) => {
						if (res.confirm) {
							let zjssq = this.gshZjssq()
							let params = {
								rentId: this.paramsVal.rentId,
								nsrId: this.paramsVal.nsrId,
								czrId: this.paramsVal.czrId,
								sqlx: this.paramsVal.nsrlx == '0' ? "01" : "02",
								rentSsqq: zjssq.rentSsqq,
								rentSsqz: zjssq.rentSsqz,
								sqyxqz: this.formData.sqyxqz
							}
							let res = await saveFpSqxx(params)
							if (res.body && res.head.errorCode == "0") {
								this.$commonCzwjs.goPage({
									url: "/pages/dkfpsq/index"
								})
							}
							// else{
							// 	uni.showModal({
							// 		title: '提示',
							// 		content: res.head.errorMsg,
							// 		confirmColor: '#000000', //确定字体颜色
							// 		showCancel: false, //没有取消按钮的弹框 
							// 		buttonText: '确定',
							// 		success: function(res) {
							// 			if (res.confirm) {
											
							// 			}
							// 		}
							// 	});
							// }
						} else {

						}
					}
				});
			}
		}
	}
</script>


<style>
	.main {
		padding: 20rpx;
		min-height: 100vh;
		/* margin-bottom: 20rpx; */
	}

	.box {
		margin: 20rpx 0;
		border-radius: 20rpx;
		overflow: hidden;
		border: 1px solid #0000006b;
	}

	.title {
		font-size: 30rpx;
		display: flex;
		justify-content: space-between;
		padding: 20rpx 0 0 0;
		align-items: center;
		font-weight: bold;

	}

	.item {
		display: flex;
		font-size: 26rpx;
		background-color: #ffffff;
		padding: 20rpx;
		word-break: break-all;
		font-weight: bold;
	}

	.xzk {
		font-size: 32rpx;
		border-bottom: 2rpx solid #0000001a;
	}

	.item>.name {
		text-align: left;
		width: 160rpx;
		margin-right: 20rpx;
		color: #66666699;
	}

	.item>.val {
		text-align: left;

	}

	.conSearch {
		background: #ffffff;
		border-top-right-radius: 20rpx;
		border-top-left-radius: 20rpx;
		padding: 20rpx;
	}


	.conSearch>.xzItem {
		padding: 20rpx;
		font-size: 26rpx;
	}

	.xzItem>.con {
		padding: 20rpx 0;
		font-size: 30rpx;
		border-bottom: 2rpx solid #0000001a;
		display: flex;
		justify-content: space-between;
		align-items: center;
		min-height: 34rpx;
	}

	.btn {
		text-align: center;
		width: 100%;
		padding: 10rpx 0 0 0;
	}

	/* 	.gddw {
		position: fixed;
		bottom: 0;
		transform: translateX(50%);
		right: 50%;
		background-color: #0000001c;
	} */

	.btn uni-button {
		width: 590rpx;
		height: 70rpx;
		line-height: 70rpx;
	}

	.bsqr {
		display: flex;
	}
</style>